package org.tailor

class FabricController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [fabricInstanceList: Fabric.list(params), fabricInstanceTotal: Fabric.count()]
    }

    def create = {
        def fabricInstance = new Fabric()
        fabricInstance.properties = params
        return [fabricInstance: fabricInstance]
    }

    def save = {
        def fabricInstance = new Fabric(params)
        if (fabricInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'fabric.label', default: 'Fabric'), fabricInstance.id])}"
            redirect(action: "show", id: fabricInstance.id)
        }
        else {
            render(view: "create", model: [fabricInstance: fabricInstance])
        }
    }

    def show = {
        def fabricInstance = Fabric.get(params.id)
        if (!fabricInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'fabric.label', default: 'Fabric'), params.id])}"
            redirect(action: "list")
        }
        else {
            [fabricInstance: fabricInstance]
        }
    }
	def renderImage = {
		def fabricInstance = Fabric.get(params.id)
		  if (fabricInstance!=null) {
			 response.setContentLength(fabricInstance.fabricPicture.length)
			 byte[] image =fabricInstance.fabricPicture
			  response.outputStream.write(image)
		 }
		 else {
			 response.setContentType("image/jpeg")
			 response.getOutputStream().write("No Image".bytes)
		 }
	 }
  
	def renderFabricImage={
		def shirt = Shirt.get(params.id)
		List fabric = Fabric.executeQuery("from shirt_fabric where shirt_fabric_id = '"+params.id+"' order by id")
		if (fabric!=null && (!fabric.isEmpty())) {
			response.setContentLength(fabric.get(0).fabricPicture.length)
			byte[] image =fabric.get(0).fabricPicture
			response.outputStream.write(image)
		}
		else {
			response.setContentType("image/jpeg")
			response.getOutputStream().write("No Image".bytes)
		}
		
		}

    def edit = {
        def fabricInstance = Fabric.get(params.id)
		if (!fabricInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'fabric.label', default: 'Fabric'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [fabricInstance: fabricInstance]
        }
    }

    def update = {
        def fabricInstance = Fabric.get(params.id)

		def fabricPicture=fabricInstance.fabricPicture
        if (fabricInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (fabricInstance.version > version) {
                    
                    fabricInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'fabric.label', default: 'Fabric')] as Object[], "Another user has updated this Fabric while you were editing")
                    render(view: "edit", model: [fabricInstance: fabricInstance])
                    return
                }
            }
            fabricInstance.properties = params
			if(params.fabricPicture.getSize()== 0){
				fabricInstance.fabricPicture = fabricPicture
				}
            if (!fabricInstance.hasErrors() && fabricInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'fabric.label', default: 'Fabric'), fabricInstance.id])}"
                redirect(action: "show", id: fabricInstance.id)
            }
            else {
                render(view: "edit", model: [fabricInstance: fabricInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'fabric.label', default: 'Fabric'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def fabricInstance = Fabric.get(params.id)
        if (fabricInstance) {
            try {
                fabricInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'fabric.label', default: 'Fabric'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'fabric.label', default: 'Fabric'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'fabric.label', default: 'Fabric'), params.id])}"
            redirect(action: "list")
        }
    }
}
